package com.my.hzb.order.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.my.hzb.order.bean.OrderInfos;
import com.my.hzb.order.mq.MqSender;
import com.my.hzb.pro.service.ProductService;
import com.my.hzb.user.service.AddressService;
import com.framelib.utils.Page;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.entiy.Address;
import com.my.hzb.entiy.Orders;
import com.my.hzb.entiy.OrdersExample;
import com.my.hzb.entiy.OrdersExample.Criteria;
import com.my.hzb.entiy.Product;
import com.my.hzb.entiy.UserMessage;
import com.my.hzb.mapper.OrdersMapper;

public class OrderServiceImpl implements OrderService{

	private static final Logger log = LogManager.getLogger(OrderServiceImpl.class);
	
	@Autowired
	private OrdersMapper orderMapper;
	@Autowired
	private AddressService addressService;
	@Autowired
	private ProductService productService;
	@Autowired
	private MqSender mqSender;
	
	public ResultBean testOrder(Integer proId,Integer adsId){
		ResultBean result = new ResultBean();
		Address ads = null;//addressService.getOne(adsId);
		Product pro = null;//productService.getOne(proId);
		if(null!=ads&&null!=pro){
			result.setReturnObj(new OrderInfos(ads,pro,36D,3));
		}
		return result;
	}
	@Override
	public void add(Orders order) {
		if(null!=order){
			order.setUpdateTime(new Date());
			order.setCreateTime(new Date());
			System.out.println("insert order number="+orderMapper.insert(order));
		}
	}

	@Override
	public void update(Orders order) {
		System.out.println("update order number="+orderMapper.updateByPrimaryKey(order));
	}

	@Override
	public void delete(Orders order) {
		
	}

	@Override
	public Orders getOne(Integer id) {
		return null;
	}

	@Override
	public List<Orders> find(Orders order,Page page) {
		OrdersExample example = new OrdersExample();
		Criteria cr = example.createCriteria();
		if(null!=order&&null!=order.getOrderNo()){
			cr.andOrderNoEqualTo(order.getOrderNo());
		}
		if(null!=order&&null!=order.getStatus()){
			cr.andStatusEqualTo(order.getStatus());
		}
		if(null!=order&&null!=order.getUserName()){
			cr.andUserNameEqualTo(order.getUserName());
		}
		if(null!=page){
			example.setPage(page);	
		}
		return orderMapper.selectByExample(example);
	}

	@Override
	public ResultBean initOrderInfo(String userName,Integer proId) {
		ResultBean result = new ResultBean();
		Address ads = getAds(userName);//new OrderHttpServiceImpl().getAdsByName(userName);
		Product pro = getPor();//null;
//		if(null==proId){
//			pro = new OrderHttpServiceImpl().getHotPro();
//		}else{
//			pro = new OrderHttpServiceImpl().isPro(proId);
//		}
		if(null!=ads&&null!=pro){
			result.setReturnObj(new OrderInfos(ads,pro,36D,3));
		}
		return result;
	}
	public Product getPor(){
		return productService.getHot();
	}
	public Address getAds(String name){
		Address ads = new Address();
		ads.setUserName(name);
		List<Address> list = addressService.find(ads,null);
		if(null!=list){
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean updateStock(Integer proId, Integer type, Integer stock) {
		try{
			log.info("proId="+proId+",type="+type+",stock="+stock);
			if(null!=proId&&null!=type&&null!=stock){
				return productService.updateStock(proId, type, stock)>0?true:false;
			}
		}catch(Exception e){
			log.error("更新库存失败",e);
		}
		return false;
	}
	@Override
	public int countByExample(Orders order) {
		OrdersExample example = new OrdersExample();
		Criteria cr = example.createCriteria();
		if(null!=order&&null!=order.getOrderNo()){
			cr.andOrderNoEqualTo(order.getOrderNo());
		}
		if(null!=order&&null!=order.getStatus()){
			cr.andStatusEqualTo(order.getStatus());
		}
		if(null!=order&&null!=order.getUserName()){
			cr.andUserNameEqualTo(order.getUserName());
		}
		return orderMapper.countByExample(example);
	}
	@Override
	public Double countPrice(Orders order) {
		OrdersExample example = new OrdersExample();
		Criteria cr = example.createCriteria();
		if(null!=order&&null!=order.getOrderNo()){
			cr.andOrderNoEqualTo(order.getOrderNo());
		}
		if(null!=order&&null!=order.getStatus()){
			cr.andStatusEqualTo(order.getStatus());
		}
		if(null!=order&&null!=order.getUserName()){
			cr.andUserNameEqualTo(order.getUserName());
		}
		return orderMapper.countPrice(example);
	}
	@Override
	public ResultBean subOrder(String userName,Integer adsId,Integer productId
			,Double orderPrice,Integer orderNumber) {
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			Orders order = new Orders();
			Product pro = productService.getHot();//new OrderHttpServiceImpl().isPro(productId);
			if(null==pro||pro.getStock()<=0){
				result.setRespMsg("商品不存在或者库存不足");
				return result;
			}
			order.setProId(productId);
			order.setOrderNo(UUID.randomUUID().toString());
			order.setPrice(orderPrice);
			order.setTimeOut(600);
			order.setSum(orderNumber);
			order.setUserName(userName);
			order.setStatus(1);
			this.add(order);
			boolean flag = this.updateStock(productId,0,orderNumber);
			//减掉库存
			log.info("回调商品减库存："+flag);
			if(flag){
				UserMessage user = new UserMessage();
				user.setMessage("订单号："+order.getOrderNo()+"订单提交成功");
				user.setUserName(userName);
				user.setStatus(0);
				user.setType(0);
				user.setCreateTime(new Date());
				user.setUpdateTime(new Date());
				mqSender.sendMessage(JsonUtil.g.toJson(user));
			}
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			result.setRespMsg("ok");
			result.setReturnStr("subOrder?orderNo="+order.getOrderNo());
		}catch(Exception e){
			log.error("提交订单失败",e);
		}
		return result;
	}
	@Override
	public ResultBean payOrder() {
		// TODO Auto-generated method stub
		return null;
	}

}
